<?php

class RoleController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'manage';

    public function actionManage()
    {
        $model = new Role('search');
        if (isset($_GET['Role'])) {
            $model->attributes = $_GET['Role'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));
    }

    public function actionCreate()
    {
        $model = new Role;
        if (isset($_POST['Role'])) {
            $model->attributes = $_POST['Role'];
            if ($model->save()) {
                if (isset($_POST['Role']['perms'])) {
                    foreach ($_POST['Role']['perms'] as $perm_id) {
                        $rp = new RolePermission();
                        $rp->rid = $model->id;
                        $rp->pid = $perm_id;
                        $rp->save();
                    }
                }
                if (isset($_POST['Role']['menus'])) {
                    foreach ($_POST['Role']['menus'] as $mid) {
                        $rm = new RoleMenu();
                        $rm->rid = $model->id;
                        $rm->mid = $mid;
                        $rm->save();
                    }
                }
                if (isset($_POST['Role']['auths'])) {
                    foreach ($_POST['Role']['auths'] as $aid) {
                        $ra = new RoleAuth();
                        $ra->rid = $model->id;
                        $ra->aid = $aid;
                        $ra->save();
                    }
                }
                $this->redirect(array($this->defaultAction));
            }
        }
        $this->render('model', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $model = Role::model()->with('users')->findByPk($id);
            if (empty($model->users)) {
                if ($model->delete()) {
                    RolePermission::model()->deleteAllByAttributes(array('rid' => $model->id));
                    RoleMenu::model()->deleteAllByAttributes(array('rid' => $model->id));
                    RoleAuth::model()->deleteAllByAttributes(array('rid' => $model->id));
                    echo 'Success';
                } else {
                    echo 'Delete Failure!';
                }
            } else {
                echo 'Exist Users of The Role';
            }
        } else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionUpdate()
    {
        if (isset($_GET['id'])) {
            $model = Role::model()->findByPk($_GET['id']);
            if ($model) {
                if (isset($_POST['Role'])) {
                    $model->attributes = $_POST['Role'];
                    if ($model->save()) {
                        RolePermission::model()->deleteAllByAttributes(array('rid' => $model->id));
                        if (isset($_POST['Role']['perms'])) {
                            foreach ($_POST['Role']['perms'] as $perm_id) {
                                $rp = new RolePermission();
                                $rp->rid = $model->id;
                                $rp->pid = $perm_id;
                                $rp->save();
                            }
                        }
                        RoleMenu::model()->deleteAllByAttributes(array('rid' => $model->id));
                        if (isset($_POST['Role']['menus'])) {
                            foreach ($_POST['Role']['menus'] as $mid) {
                                $rm = new RoleMenu();
                                $rm->rid = $model->id;
                                $rm->mid = $mid;
                                $rm->save();
                            }
                        }
                        RoleAuth::model()->deleteAllByAttributes(array('rid' => $model->id));
                        if (isset($_POST['Role']['auths'])) {
                            foreach ($_POST['Role']['auths'] as $aid) {
                                $ra = new RoleAuth();
                                $ra->rid = $model->id;
                                $ra->aid = $aid;
                                $ra->save();
                            }
                        }
                        $this->redirect(array($this->defaultAction));
                    }
                }
                $this->render('model', array(
                    'model' => $model,
                ));
            } else {
                $this->redirect(array($this->defaultAction));
            }
        } else {
            $this->redirect(array($this->defaultAction));
        }

    }

    // Uncomment the following methods and override them if needed
    /*
    public function filters()
    {
        // return the filter configuration for this controller, e.g.:
        return array(
            'inlineFilterName',
            array(
                'class'=>'path.to.FilterClass',
                'propertyName'=>'propertyValue',
            ),
        );
    }

    public function actions()
    {
        // return external action classes, e.g.:
        return array(
            'action1'=>'path.to.ActionClass',
            'action2'=>array(
                'class'=>'path.to.AnotherActionClass',
                'propertyName'=>'propertyValue',
            ),
        );
    }
    */
}